const babel = require('@babel/core');
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;

// 定义默认规则配置
const defaultRules = {
    'no-var': true,
    'no-console': true
};

// 简单的 ESLint 规则实现
function customESLint(code, userRules = {}) {
    // 合并用户规则和默认规则
    const rules = { ...defaultRules, ...userRules };
    
    const ast = parser.parse(code, {
        sourceType: 'module',
        plugins: ['jsx']
    });

    const problems = [];

    traverse(ast, {
        VariableDeclaration(path) {
            if (rules['no-var'] && path.node.kind === 'var') {
                problems.push({
                    ruleId: 'no-var',
                    message: 'Use const or let instead of var',
                    line: path.node.loc.start.line,
                    column: path.node.loc.start.column
                });
            }
        },

        CallExpression(path) {
            if (
                rules['no-console'] &&
                path.node.callee.type === 'MemberExpression' &&
                path.node.callee.object.name === 'console'
            ) {
                problems.push({
                    ruleId: 'no-console',
                    message: 'Unexpected console statement',
                    line: path.node.loc.start.line,
                    column: path.node.loc.start.column
                });
            }
        }
    });

    return problems;
}

// 测试代码
const testCode = `
var x = 1;
console.log('test');
let y = 2;
`;

// 使用自定义规则配置
const customRules = {
    'no-var': true,
    'no-console': false  // 禁用 console 检查
};

const results = customESLint(testCode, customRules);
console.log('ESLint Problems:');
results.forEach(problem => {
    console.log(`${problem.ruleId}: Line ${problem.line}, Column ${problem.column}: ${problem.message}`);
});

module.exports = customESLint;